<?php


namespace Hilaoyu\LaravelExtend\Services\Realnames;


use Hilaoyu\LaravelPrivateApi\PrivateApiClient;

class RealnameApiClient extends PrivateApiClient
{
    protected $client_config = [];
    public function __construct($client_config)
    {
        $this->client_config = $client_config;
        $uri = data_get($client_config,'service_uri');
        $secret = data_get($client_config,'secret');
        $app_id = data_get($client_config,'app_id');

        $uriArr = $domain = parse_url($uri);
        parent::__construct($uri, $secret, $app_id);
    }
    
    public function save(array $params){

        if($this->request(data_get($this->client_config,'paths.save'),$params)){
            return $this->getResult('data');
        }
        return [];
    }
    public function find(array $where){

        if($this->request(data_get($this->client_config,'paths.find'),$where)){
            $data = $this->getResult('data');
            if($data){
                $entity = new RealnamesEntity();
                $model = $entity->model(data_get($data,'id',''),true);

                $model->fill($data);
                if(!$model->save()){
                    $this->setError($model->getError());
                    return null;
                }
                return $model;
            }
        }
        return null;
    }
    public function cardTypeOptions(array $where){

        $params = [
            'selected' => $selected,
            'add_title' => $add_title,
        ];
        if($this->request(data_get($this->client_config,'paths.cardTypeOptions'),$params)){
            return $this->getResult('data');
        }
        return [];
    }

    public function fileUploaders(){

        if($this->request(data_get($this->client_config,'paths.uploaders'))){
            return $this->getResult('data');
        }
        return null;
    }

    public function finUserRealname($user_id){

        if($this->request(data_get($this->client_config,'paths.userRealName'),['user_id'=>$user_id])){
            $data = $this->getResult('data');
            if($data){
                $entity = new RealnamesEntity();
                $model = $entity->find(['user_id'=>$user_id]);
                if(!$model){
                    $model = $entity->model(data_get($data,'id',''),true);
                }

                $model->fill($data);
                if(!$model->save()){
                    $this->setError($model->getError());
                    return null;
                }
                return $model;
            }
        }
        return null;
    }


}